﻿Imports System.Data.SqlClient
Public Class Proce
    'weather
    Public idProce As Integer
    Public numProce As Integer
    Public code As Integer
    Public annee As Integer
    Public dateProce As Date
    Public sujet As String
    Public src As source
    Public ref As Reference
    Public observation As String
    Public caracClt As String
    Public caracAdv As String
    Public tribunal As String
    Public dateAchev As DateTime
    Public plt As Plainte
    Public envois As List(Of Envoi)

    Public Sub New()
        Me.envois = New List(Of Envoi)
        Me.src = New source
        Me.ref = New Reference
    End Sub


    Public Sub New(ByVal numProc As Integer, ByVal code As Integer, ByVal annee As Integer, ByVal tribunal As String, ByVal dateProce As Date _
    , ByVal sujet As String, ByVal observation As String, ByVal caracClt As String, _
    ByVal caracAdv As String, Optional ByVal dateAchev As DateTime = Nothing, Optional ByVal iddProc As Integer = Nothing)

        Me.idProce = iddProc
        Me.numProce = numProc
        Me.code = code
        Me.annee = annee
        Me.tribunal = tribunal
        Me.dateProce = dateProce
        Me.sujet = sujet
        Me.src = New source
        Me.ref = New Reference
        Me.envois = New List(Of Envoi)
        Me.observation = observation
        Me.caracClt = caracClt
        Me.caracAdv = caracAdv
        Me.dateAchev = dateAchev
    End Sub

    'Public Sub remplirSource(ByVal idSource As Integer)
    '    Partager.cmd = New SqlCommand("select * from Source Where id = " & idSource, Partager.con)
    '    Partager.open()
    '    Partager.dr = Partager.cmd.ExecuteReader()
    '    Me.src = New source
    '    If Partager.dr.HasRows Then
    '        While Partager.dr.Read
    '            With Me.src
    '                .id = Partager.dr.GetInt32(0)
    '                .nom = Partager.dr.GetString(1)
    '            End With
    '        End While
    '    End If
    'End Sub

    'Public Sub remplirReference(ByVal idRef As Integer)
    '    Partager.cmd = New SqlCommand("select * from Reference where Ref = " & idRef, Partager.con)
    '    Partager.open()
    '    Partager.dr = Partager.cmd.ExecuteReader()
    '    Me.ref = New Reference
    '    If Partager.dr.HasRows Then
    '        While Partager.dr.Read
    '            With Me.ref
    '                .Ref = Partager.dr.GetInt32(0)
    '                .dateInscriptionBur = Partager.dr.GetDateTime(1)
    '            End With
    '        End While
    '    End If
    'End Sub

    Public Sub ajouterRelPlt(ByVal idPlt As Integer)
        Partager.cmd = New SqlClient.SqlCommand("INSERT INTO [procPlaint] ([idProc],[idPlt]) VALUES(" & Me.idProce & _
                                                "," & idPlt & ")", Partager.con)

        Partager.open()
        Partager.cmd.ExecuteNonQuery()
        Partager.close()

    End Sub

    Public Sub deleteRelPlt()
        Partager.cmd = New SqlClient.SqlCommand("delete from procPlaint where idProc = " & Me.idProce, Partager.con)

        Partager.open()
        Partager.cmd.ExecuteNonQuery()
        Partager.close()

    End Sub

    Public Sub remplirLstEnvois()
        Me.envois.Clear()
        Dim cmd As New SqlClient.SqlCommand("select * from Envois where Id in (" & _
                                            "select idEnvoi from procesEnvois " & _
                                            "where idProc =" & Me.idProce & ")", Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        Dim strDat As String = ""
        While Partager.dr.Read
            If Partager.dr("Date_Envoi") = "01/01/1900" Then
                strDat = ""
            Else
                strDat = Partager.dr("Date_Envoi")
            End If

            Me.envois.Add(New Envoi(Partager.dr.GetInt32(0), Partager.dr.GetString(1), strDat, Partager.dr.GetString(3), _
                                    Partager.dr.GetString(4), Partager.dr.GetString(5)))
        End While
        Partager.dr.Close()
        Partager.close()
    End Sub
    Public Sub AjouterEnvoi(ByVal idEnvoi As Integer)
        Partager.cmd = New SqlClient.SqlCommand("INSERT INTO [procesEnvois]([idProc],[idEnvoi]) VALUES(" & Me.idProce & "," & idEnvoi & ")", Partager.con)

        Partager.open()
        Partager.cmd.ExecuteNonQuery()
        Partager.close()
    End Sub

    Public Sub supprimerEnvoi(ByVal idEnvoi As Integer)
        Partager.cmd = New SqlClient.SqlCommand("delete from [procesEnvois] where idEnvoi = " & idEnvoi, Partager.con)

        Partager.open()
        Partager.cmd.ExecuteNonQuery()
        Partager.close()
    End Sub
End Class
